package com.common.base.app.activity

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.viewbinding.ViewBinding
import com.common.base.app.extras.*
import com.common.base.databinding.BaseActivityNavBinding
import com.common.base.widget.PlaceholderView

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/19
 * <p>
 * 描述：带标题的基类
 * <p>
 * 修订历史：
 */
abstract class BaseNavActivity<VB : ViewBinding> : BaseVBActivity<BaseActivityNavBinding>() {

    override fun getPlaceholderView(): PlaceholderView? = mViewBinding.mPlaceholderView

    override fun getViewBinding() = BaseActivityNavBinding.inflate(layoutInflater)

    protected lateinit var mViewContentBinding: VB

    override fun initViews() {
        super.initViews()
        // 设置是否展示标题栏
        setShowToolBar(isShowToolBar())
        //设置返回按钮
        isShowBackIcon().yes {
            setVisible(mViewBinding.toolbarInclude.backViewTotal)
            mViewBinding.toolbarInclude.backViewTotal.setOnClickListener { onBackClick() }
        }
        // 设置填充容器
        if (mViewBinding.navLayout.childCount > 0) {
            mViewBinding.navLayout.removeAllViews()
        }
        mViewContentBinding = getViewContentBinding()
        mViewBinding.navLayout.addView(mViewContentBinding.root)
        if (isShowToolBar()) {
            val rightTxt = getRightText()
            rightTxt.isNoEmpty().yes {
                setVisible(mViewBinding.toolbarInclude.tvRight)
                setText(mViewBinding.toolbarInclude.tvRight, rightTxt)
            }
            setPageTitle(getPageTitle())
        }
    }


    protected fun setPageTitle(title: String?) {
        mViewBinding.toolbarInclude.toolbar.title = ""
        setTextViewBold(mViewBinding.toolbarInclude.titleTv)
        setText(mViewBinding.toolbarInclude.titleTv, title)
    }

    open fun getRightText(): String = ""

    fun getToolbarView(): Toolbar {
        return mViewBinding.toolbarInclude.toolbar
    }
    fun getTitleTextView(): TextView {
        return mViewBinding.toolbarInclude.titleTv
    }

    fun getRightTextView(): TextView {
        return mViewBinding.toolbarInclude.tvRight
    }

    fun getRightImageView(): ImageView {
       return mViewBinding.toolbarInclude.ivRight
    }

    fun setOnRightViewClickListener(clickListener: View.OnClickListener) {
        mViewBinding.toolbarInclude.tvRight.setOnClickListener(clickListener)
        mViewBinding.toolbarInclude.ivRight.setOnClickListener(clickListener)
    }

    /**
     * 执行返回操作
     */
    protected open fun onBackClick() {
        finish()
    }

    /**
     * 设置ToolBar的展示状态
     * @param isShow 是否展示
     */
    private fun setShowToolBar(isShow: Boolean) {
        setVisibility(mViewBinding.appBarLayout, isShow)
        setVisibility(mViewBinding.statusView, isShow)
    }

    protected open fun getPageTitle(): String? = null

    /**
     * 是否展示ToolBar，如果设置为false则不展示。
     * 如果不展示标题栏，则状态栏也不会展示。
     */
    protected open fun isShowToolBar(): Boolean = true

    /**
     * 是否可以返回，如果可以则展示返回按钮，并且设置返回事件
     * 默认可以返回
     */
    protected open fun isShowBackIcon(): Boolean = true


    /**
     * 子类继续填充内容容器布局
     */
    abstract fun getViewContentBinding(): VB

    open fun onBack(view: View? = null) {
        finish()
    }
}